package com.example.demo.controller;

import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-06-05
 * Time: 9:33
 */
@RestController
@RequestMapping("/user2")
public class UserController2 {

    @Autowired
    private UserService userService;

    @RequestMapping("/test")
    public int test(Integer id) throws InterruptedException {
        return del(id);
    }

    /**
     * 该方法开启事务, 如果发生异常则进行事务回滚.
     * 如果正常执行, 则提交事务
     */
    @RequestMapping("/del2")
    @Transactional
    public int del2(Integer id) throws InterruptedException {
        if (id == null || id <= 0) return 0;

        int result = 0;
        result = userService.del(id);
        return result;  // 正常查询以后再页面上没有显示查询的 result 结果
    }

    @RequestMapping("del")
    @Transactional(timeout = 3)
    public int del(Integer id) throws InterruptedException {
        if (id == null || id <= 0) return 0;
        return userService.del(id);
    }
}
